<?php
/**
 * 点赞和关注接口
 */
require_once "login_filter.php";
$my_uuid=$Auth['uuid'];
//$my_uuid="94B96C11-4DB2-E406-8B6D-F4EB86EB";
use app\cls\tom\Response;
use app\cls\tom\SysResource;
if($_REQUEST['type']==0){
    //这是点赞
    if(empty($_REQUEST['video_uuid']) || empty($_REQUEST['uuid'])){
        Response::output(-1,"参数错误fa");
    }
    $type="fabulous";
}else if($_REQUEST['type']==1){
    //这是关注
    if(empty($_REQUEST['uuid'])){
        Response::output(-1,"参数错误fo");
    }
    $type="follow";
}else{
    Response::output(-1,"参数错误1");
}

$data['my_uuid']=$my_uuid;
if(!empty($_REQUEST['video_uuid'])){
    $data['video_uuid']=$_REQUEST['video_uuid'];
}
if(!empty($_REQUEST['uuid'])){
    $data['other_uuid']=$_REQUEST['uuid'];
}
if(!empty($_REQUEST['select_id'])){
    $data['other_id']=$_REQUEST['select_id'];
}

//实例化redis
$redis = SysResource::getRedis();
$suffix=rand(1,5);
$uid=sprintf("%d",crc32($data['my_uuid']));//自己的uuid
$oid=sprintf("%d",crc32($data['other_uuid']));//他人的uuid
//正式环境的key
$key = REDIS_PREFIX."{$type}_".$suffix;
$key1=REDIS_PREFIX."{$type}_".$uid;
$key11=REDIS_PREFIX."is_{$type}_".$oid;
$user_material_love_key=REDIS_PREFIX."user_material_love_".$suffix;//用户视频被点赞

//测试环境的key
//$key = REDIS_PREFIX."test_{$type}_".$suffix;
//$key1=REDIS_PREFIX."test_{$type}_".$uid;
//$key11=REDIS_PREFIX."test_is_{$type}_".$oid;
//$user_material_love_key=REDIS_PREFIX."test_user_material_love_".$suffix;//用户视频被点赞
//if($_REQUEST['type']==0){
////    $key1=REDIS_PREFIX."{$type}_".$uid;//正式
//    $key1=REDIS_PREFIX."test_{$type}_".$uid;//测试
//}else if($_REQUEST['type']==1){
//    //正式
////    $key1=REDIS_PREFIX."{$type}_".$uid;//这是我关注的列表
////    $key11=REDIS_PREFIX."is_{$type}_".$oid;//这是他人的粉丝列表
//    //测试
//    $key1=REDIS_PREFIX."test_{$type}_".$uid;//这是我关注的列表
//    $key11=REDIS_PREFIX."test_is_{$type}_".$oid;//这是他人的粉丝列表
//}else{
//    Response::output(-1,"参数错误2");
//}
//1.点赞存的是视频的video_uuid,关注存的是用户的uuid
    if($_REQUEST['type']==0){
    //2.先去redis里查询是否存在这个点赞了，存在就不添加进队列了
        $r1=$redis->hKeys($key1);
        if(in_array($data['video_uuid'],$r1)){
            //存在
            Response::output(0,"添加成功",$type."_".$suffix);
        }else{
            //不存在就添加
            $time=date("Y-m-d H:i:s",time());
            $redis->hset($key1,$data['video_uuid'],$time);
        }
        //2.1再把用户点赞的视频同步到用户上传表的字段，先入队列
        $user_material_love_key_data['video_uuid']=$data['video_uuid'];
        $user_material_love_key_data['id']=$data['other_id'];
        $redis->lPush($user_material_love_key,json_encode($user_material_love_key_data));
    }else if($_REQUEST['type']==1){
        //3.先去redis里查询是否存在这个关注了，存在就不添加进队列了
        $r1=$redis->hKeys($key1);
        if(in_array($data['other_uuid'],$r1)){
            //存在
            Response::output(0,"添加成功",$type."_".$suffix);
        }else{
            //不存在就添加
            $time=date("Y-m-d H:i:s",time());
            $redis->hset($key1,$data['other_uuid'],$time);
            //这是别人的粉丝hash里是否有我了
            $r2=$redis->hKeys($key11);
            //不存在就添加
            if(!in_array($data['my_uuid'],$r2)){
                $redis->hset($key11,$data['my_uuid'],$time);
            }
        }
    }
//2.再把数据入队列
$redis->lPush($key,json_encode($data));
Response::output(0,"添加成功",$type."_".$suffix);


